using System;
using System.Collections;
using System.Collections.Generic;
using Platform.CharacterData;
using UnityEngine;
using UnityEngine.UI;

namespace Platform
{
    public class HealthBarUI : MonoBehaviour
    {
        private Entity entity;
        private RectTransform rectTransform;
        private Slider slider;
        private CharacterStats myStats;

        private void Start()
        {
            rectTransform = GetComponent<RectTransform>();
            slider = GetComponent<Slider>();

            entity = GetComponentInParent<Entity>();
            myStats = GetComponentInParent<CharacterStats>();

            entity.OnFlip += OnFlip;
            myStats.OnHealthChange += UpdateUI;

            UpdateUI();
        }

        private void OnDestroy()
        {
            entity.OnFlip -= OnFlip;
            myStats.OnHealthChange -= UpdateUI;
        }


        private void UpdateUI()
        {
            slider.maxValue = myStats.MaxHealth;
            slider.value = myStats.CurrentHealth;
        }

        private void OnFlip()
        {
            rectTransform.Rotate(0, 180, 0);
        }
    }
}